Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Improve CodeSyntaxHighlight object #268

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

tlambert03
Copy link
Member

This PR improves a number of things and fixes some slight issues with CodeSyntaxHighlight:

  • adds full type hinting, including KnownStyle for IDE suggestions of valid pygments names
  • prevents Formatter from overriding all font selections on the parent object. I tried to apply fonts and found that get_text_char_format was always clobbering whatever I did. Furthermore, it wasn't successfully applying "Monospace", instead I get: WARNING: Populating font family aliases took 75 ms. Replace uses of missing font family "Monospace" with one that exists to avoid this cost.. So this adds a full font family tree that should work on any system, and it only applies it if the style explicitly sets the "mono" flag. This means that most users will now need to set Monospace themselves (but since it already wasn't working on, macos for example, this is probably a good thing).
  • allows the user to pass in any QObject that supports document(), simplifying the usage, and also allowing allowing us to automatically set the background color on objects that support setPalette.
  • adds setTheme and setLanguage methods that allow you to change the style on an existing formatter, including updating the background color on supported objects.
  • adds lots of docstrings to explain what's going on

@tlambert03 tlambert03 requested a review from Czaki December 23, 2024 17:14
Copy link

codecov bot commented Dec 23, 2024

Codecov Report

Attention: Patch coverage is 96.00000% with 2 lines in your changes missing coverage. Please review.

Project coverage is 87.01%. Comparing base (4da5ac2) to head (35819c0).

Files with missing lines Patch % Lines
src/superqt/utils/_code_syntax_highlight.py 96.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #268      +/-   ##
==========================================
- Coverage   87.12%   87.01%   -0.12%     
==========================================
  Files          46       46              
  Lines        3441     3465      +24     
==========================================
+ Hits         2998     3015      +17     
- Misses        443      450       +7     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@tlambert03
Copy link
Member Author

I assume the two failing napari tests are unrelated, but @Czaki could you confirm?

@Czaki
Copy link
Contributor

Czaki commented Dec 24, 2024

I assume the two failing napari tests are unrelated, but @Czaki could you confirm?

Yes. It is a recent contribution to napari.
I think that test are failing because you do not set up windows manager https://pytest-qt.readthedocs.io/en/latest/troubleshooting.html#xvfb-assertionerror-timeouterror-when-using-waituntil-waitexposed-and-ui-events

Maybe we should think to also easy skip test marked as skip_local_focus on other CI.

Copy link
Contributor

@Czaki Czaki left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

"Consolas",
"Andale Mono",
"Source Code Pro",
"monospace",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that it may be worth to and Ubuntu Mono - the default mono font on Ubuntu system.

def palette(self) -> QPalette: ...
def setPalette(self, palette: QPalette) -> None: ...

KnownStyle: TypeAlias = Literal[
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any plan for trace pigments style updates (ex compare pygments.styles.STYLES content witrh this annotation in tests) or just wait for issues?

Copy link
Member Author

@tlambert03 tlambert03 Dec 24, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no plan... but I'll mention that the type hint here is actually:

theme: KnownStyle | str = "default",

which means that users will get the autocomplete suggestions, but it's not an error to enter something that is not in the KnownStyle list. So the only "issue" we might see is that someone didn't get a nice autocomplete hint :) (but mypy won't mind)

Comment on lines 127 to 129
text_char_format.setForeground(QColor(f"#{color}"))
if bgcolor := style.get("bgcolor"):
text_char_format.setBackground(QColor(bgcolor))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why one with hash, and the other without?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good point! it was already that way (on main):

if style.get("color"):
text_char_format.setForeground(QtGui.QColor(f"#{style['color']}"))
if style.get("bgcolor"):
text_char_format.setBackground(QtGui.QColor(style["bgcolor"]))

you wrote that ... do you recall any reason for it?

@tlambert03
Copy link
Member Author

you do not set up windows manager https://pytest-qt.readthedocs.io/en/latest/troubleshooting.html#xvfb-assertionerror-timeouterror-when-using-waituntil-waitexposed-and-ui-events

i do use https://github.com/pyvista/setup-headless-display-action ... which replaces all three of tlambert03/setup-qt-libs@v1, powershell gl-ci-helpers/appveyor/install_opengl.ps1, and aganders3/headless-gui@v2. You can see it run here: https://github.com/pyapp-kit/superqt/actions/runs/12485275920/job/34843848003?pr=268

the only thing I think they don't use is herbstluftwm ... does that make all the difference here?

Maybe we should think to also easy skip test marked as skip_local_focus on other CI.

that would be useful. some sort of mark rather than a local skip. will find some workaround for now. did you also have to change your CI setup in napari when that change went in? I don't remember

@Czaki
Copy link
Contributor

Czaki commented Dec 24, 2024

the only thing I think they don't use is herbstluftwm ... does that make all the difference here?

Yes. It is required to propagate GUI events.

did you also have to change your CI setup in napari when that change went in

We have added herbstluftwm, but it is added to aganders3/headless-gui@v2

@tlambert03
Copy link
Member Author

TIL. Thanks

Added it to setup headless action in pyvista/setup-headless-display-action#25 (and that fixes it here too)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants